/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2011
 */

package com.tech15.core.action;

import static javacommon.util.extjs.Struts2JsonHelper.outString;
import static javacommon.util.extjs.Struts2JsonHelper.outXmlJson;

import java.io.File;
import java.util.Hashtable;
import java.util.Map;

import javacommon.base.BaseStruts3Action;
import javacommon.util.ResInfo;

import org.apache.struts2.ServletActionContext;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.web.scope.Flash;
import cn.org.rapid_framework.web.util.HttpUtils;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.tech15.core.model.Attach;
import com.tech15.core.service.AttachManager;
import com.tech15.core.vo.query.AttachQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */


public class AttachAction extends BaseStruts3Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	//forward paths
	protected static final String QUERY_JSP = "/pages/Attach/query.jsp";
	protected static final String LIST_JSP= "/pages/Attach/list.jsp";
	protected static final String CREATE_JSP = "/aaupload/fileupload.jsp";
	protected static final String EDIT_JSP = "/pages/Attach/edit.jsp";
	protected static final String SHOW_JSP = "/pages/Attach/show.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pages/Attach/list.do";
	
	protected static final String DOCUMENT_UPDATE = "/pub/document_update.jsp";
	private static final String DOCUMENT_BROWSE = "/pub/document_browse.jsp";
	
	private AttachManager attachManager;
	
	private Attach attach;
	java.lang.String id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			attach = new Attach();
		} else {
			attach = (Attach)attachManager.getById(id);
		}
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setAttachManager(AttachManager manager) {
		this.attachManager = manager;
	}	
	
	public Object getModel() {
		return attach;
	}
	
	public void setAttachid(java.lang.String val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	
	/** 执行搜索 */
	public String list() {
		AttachQuery query = newQuery(AttachQuery.class,DEFAULT_SORT_COLUMNS);
		
		Page page = attachManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}
	
	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	
	/** 进入新增页面*/
	public String create() {
		return "input";
	}
	
	/** 保存新增对象 */
	public String save() {
		String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
		attachManager.saveAttachs(realpath,new Long(20054229),"customer","1", getAttachFiles(), getAttachFilesFileName(),getAttachFilesContentType());
        /*if(attachFiles!=null){
        	System.out.println("=============");
        	File savedir = new File(realpath);  
            if(!savedir.exists()){   
                  savedir.mkdirs();  
            }  
            for (int i=0; i < attachFiles.size(); i++){ 
                File savefile = new File(savedir,attachFilesFileName.get(i));  
                try {
					FileUtils.copyFile(attachFiles.get(i), savefile);
				} catch (IOException e) {
					e.printStackTrace();
				}  
            }
        }else{
        	System.out.println("uploads 是空。。。。。");
        }*/
		return "suc";
	}
	
	public String browseDoc() {
		return DOCUMENT_BROWSE;
	}
	
	public void updateDoc() {
		String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
		boolean mf = attachManager.updateAttach(this.attach,realpath,getAttachFile());
		if(mf){
			outString(this.OPERATE_SUCCESS);
		}else{
			outString(this.OPERATE_FAILED);
		}
	}
	
	public String editDoc(){
		return DOCUMENT_UPDATE;
	}
	
	/**进入更新页面*/
	public String edit() {
		return EDIT_JSP;
	}
	
	/**保存更新对象*/
	public String update() {
		attachManager.update(this.attach);
		return LIST_ACTION;
	}
	
	/**删除对象*/
	public String delete() {
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			java.lang.String id = new java.lang.String((String)params.get("attachid"));
			attachManager.removeById(id);
		}
		return LIST_ACTION;
	}
	
	public void deleteById() {
		Map<String, Object> resInfo = null;
		try{
			if(attach.getUserid().longValue() == getLoginInfo().getUserid().longValue()){
				attachManager.removeById(id);
			}
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	private File attachFile;

	public File getAttachFile() {
		return attachFile;
	}

	public void setAttachFile(File attachFile) {
		this.attachFile = attachFile;
	}

}
